package com.cisystem.web.controller.business;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.cisystem.common.core.domain.entity.SysDept;
import com.cisystem.system.domain.vo.InnovationVo;
import com.cisystem.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cisystem.common.annotation.Log;
import com.cisystem.common.core.controller.BaseController;
import com.cisystem.common.core.domain.AjaxResult;
import com.cisystem.common.enums.BusinessType;
import com.cisystem.system.domain.Innovation;
import com.cisystem.system.service.IInnovationService;
import com.cisystem.common.utils.poi.ExcelUtil;
import com.cisystem.common.core.page.TableDataInfo;

/**
 * innovationController
 *
 * @author kelly
 * @date 2023-07-07
 */
@RestController
@RequestMapping("/business/innovation")
public class InnovationController extends BaseController {
    @Autowired
    private IInnovationService innovationService;

    @Autowired
    ISysDeptService deptService;
    /**
     * 查询innovation列表
     */
    @PreAuthorize("@ss.hasPermi('business:innovation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Innovation innovation) {
        startPage();
        List<Innovation> list = innovationService.selectInnovationList(innovation);
        return getDataTable(list);
    }

    /**
     * 导出innovation列表
     */
    @PreAuthorize("@ss.hasPermi('business:innovation:export')")
    @Log(title = "innovation", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Innovation innovation) {
        List<Innovation> list = innovationService.selectInnovationList(innovation);
        // deptId 转化成描述
        List<SysDept> sysDepts = deptService.selectDeptList(new SysDept());
        Map<String,String> deptDes = new HashMap<>();
        if (!CollectionUtils.isEmpty(sysDepts)){
            deptDes = sysDepts.stream().collect(Collectors.toMap(i->String.valueOf(i.getDeptId()),
                    SysDept::getDeptName,(f, b)->f));
        }
        // 状态转化
        Map<String,String> statusMap  = new HashMap<>();
        statusMap.put("1","Open");
        statusMap.put("2","Ongoing");
        statusMap.put("3","Closed");
        for (Innovation inno : list) {
            inno.setDeptId(deptDes.get(inno.getDeptId()));
            inno.setStatus(statusMap.get(inno.getStatus()));
        }
        ExcelUtil<Innovation> util = new ExcelUtil<Innovation>(Innovation.class);
        util.exportExcel(response, list, "innovation数据");
    }

    /**
     * 获取innovation详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:innovation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(innovationService.selectInnovationById(id));
    }

    /**
     * 新增innovation
     */
    @PreAuthorize("@ss.hasPermi('business:innovation:add')")
    @Log(title = "innovation", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InnovationVo innovationVo) {
        return toAjax(innovationService.insertInnovation(innovationVo));
    }

    /**
     * 修改innovation
     */
    @PreAuthorize("@ss.hasPermi('business:innovation:edit')")
    @Log(title = "innovation", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InnovationVo innovationVo) {
        return toAjax(innovationService.updateInnovation(innovationVo));
    }

    /**
     * 删除innovation
     */
    @PreAuthorize("@ss.hasPermi('business:innovation:remove')")
    @Log(title = "innovation", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(innovationService.deleteInnovationByIds(ids));
    }
}
